package com.florent37.runtime_permission;



import ohos.app.Context;
import ohos.bundle.BundleInfo;
import ohos.bundle.IBundleManager;
import ohos.global.icu.text.IDNA;
import ohos.rpc.RemoteException;

import java.util.ArrayList;
import java.util.List;

public class PermissionManifestFinder {

    public static List<String> findNeededPermissionsFromManifest(Context context) {
        final IBundleManager bundleManager= (IBundleManager) context.getAbilityPackageContext();
        BundleInfo info = null;
        try {
            info = bundleManager.getBundleInfo(context.getBundleName(), IBundleManager.GET_APPLICATION_INFO_WITH_PERMISSION);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        final List<String> needed = new ArrayList<>();
        if (info != null && info.reqPermissions != null ) {
            for (int i = 0; i < info.reqPermissions.size(); i++) {
                //harmony里没有requestedPermissionsFlags
                //final int flags = info.requestedPermissionsFlags[i];
                String group = null;
                try {
                    //没有找到group属性，暂时用name代替
                    group = bundleManager.getBundleInfo(info.reqPermissions.get(i), 0).name;
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                if (group != null) {
                    needed.add(info.reqPermissions.get(i));
                }
            }
        }
        return needed;
    }

}
